package com.fireghost.jdt.mainActivity;

import java.sql.Timestamp;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.fireghost.jdt.R;
import com.fireghost.jdt.database.JobDbEntry;
import com.fireghost.jdt.database.JournalDeTravailDbAdapter;
import com.fireghost.jdt.database.TypeDbEntry;

public class JobContentViewAdapter {
	private TextView oTxtDateFull;
	private TextView oTxtWeekNumber;
	private TextView oTxtStartEndTime;
	private TextView oTxtTotalTime;
	private TextView oTxtType;
	private TextView oTxtJobDescription;
	private ImageButton oBtnEditMode;
	private ImageButton oBtnDelJob;
	
	private ImageButton oBtnDatePicker;
	private ImageButton oBtnStartEndTimePicker;
	private Spinner oSpinTypes;
	private SpinnerTypesAdapter oSpinnerTypesAdapter;
	private ImageButton oBtnAddType;
	private EditText oTxtEditJobDescription;
	private Button oBtnSave;
	private Button oBtnCancel;
	
	private JobDbEntry oJobDbEntry;
	
	Activity mainActivity;
	JournalDeTravailDbAdapter oJdtDb;
	
	
	
	///////    Listeners
	/*
	 * Listener for oBtnDatePicker click
	 */
	private OnClickListener oBtnDatePickerClickListener = new OnClickListener() {
		public void onClick(View v) {
			GregorianCalendar oCal = new GregorianCalendar();
			oCal.setTimeInMillis(oJobDbEntry.getTimestampStart());
			
			DatePickerDialog oDatePickerDialog = new DatePickerDialog(
					mainActivity,
					oDialogDatePickerDateSetListener,
					oCal.get(GregorianCalendar.YEAR),
					oCal.get(GregorianCalendar.MONTH),
					oCal.get(GregorianCalendar.DAY_OF_MONTH));
			
			oDatePickerDialog.show();
		}
	};
	
	/*
	 * Listener for oBtnStartEndTimePicker click
	 */
	private OnClickListener oBtnStartEndTimePickerClickListener = new OnClickListener() {
		public void onClick(View v) {
			final Dialog oStartEndTimePickerDialog = new Dialog(mainActivity);
			oStartEndTimePickerDialog.setContentView(R.layout.set_start_end_time_dialog);
			
			Button oBtnOK = (Button)oStartEndTimePickerDialog.findViewById(R.id.oBtnStartEndTimeDialogOK);
			Button oBtnCancel = (Button)oStartEndTimePickerDialog.findViewById(R.id.oBtnStartEndTimeDialogCancel);
			
			final TimePicker oTimePickerStart = (TimePicker)oStartEndTimePickerDialog.findViewById(R.id.oTimePickerFrom);
			final TimePicker oTimePickerEnd = (TimePicker)oStartEndTimePickerDialog.findViewById(R.id.oTimePickerTo);
			
			final Timestamp oTimestampStart = new Timestamp(oJobDbEntry.getTimestampStart());
			final Timestamp oTimestampEnd = new Timestamp(oJobDbEntry.getTimestampEnd());
			
			
			oBtnOK.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					
					oTimestampStart.setHours(oTimePickerStart.getCurrentHour());
					oTimestampStart.setMinutes(oTimePickerStart.getCurrentMinute());
					
					oTimestampEnd.setHours(oTimePickerEnd.getCurrentHour());
					oTimestampEnd.setMinutes(oTimePickerEnd.getCurrentMinute());
					
					oJobDbEntry.setTimestampStart(oTimestampStart.getTime());
					oJobDbEntry.setTimestampEnd(oTimestampEnd.getTime());
					
					populate(oJobDbEntry);
					
					oStartEndTimePickerDialog.cancel();
				}
			});
			
			oBtnCancel.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					oStartEndTimePickerDialog.cancel();
				}
			});
			
			
			oTimePickerStart.setCurrentHour(oTimestampStart.getHours());
			oTimePickerStart.setCurrentMinute(oTimestampStart.getMinutes());

			oTimePickerEnd.setCurrentHour(oTimestampEnd.getHours());
			oTimePickerEnd.setCurrentMinute(oTimestampEnd.getMinutes());
			
			oStartEndTimePickerDialog.show();
		}
	};
	
	/*
	 * Listener for oBtnAddType click
	 */
	private OnClickListener oBtnAddTypeClickListener = new OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
		}
	};
	
	/*
	 * Listener for oBtnSave click
	 */
	private OnClickListener oBtnSaveClickListener = new OnClickListener() {
		public void onClick(View v) {
			oJdtDb.updateJob(oJobDbEntry);
			toViewMode();
		}
	};

	/*
	 * Listener for oBtnCancel click
	 */
	private OnClickListener oBtnCancelClickListener = new OnClickListener() {
		public void onClick(View v) {
			oJobDbEntry = oJdtDb.getJobByID(oJobDbEntry.getJobID());
			toViewMode();
		}
	};
	
	/*
	 * Listener for oBtnEditMode click
	 */
	private OnClickListener oBtnEditModeClickListener = new OnClickListener() {
		public void onClick(View v) {
			toEditMode();
		}
	};
	
	/*
	 * Listener for oSpinTypes item selected
	 */
	private OnItemSelectedListener oSpinTypesItemSelectedListener = new OnItemSelectedListener() {
		public void onItemSelected(AdapterView<?> arg0, View arg1, int p_iPosition,
				long p_iId) {
			oJobDbEntry.setTypeID(p_iId);
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			
		}
	};
	
	/*
	 * Listener for oBtnDelJob click
	 */
	private OnClickListener oBtnDelJobClickListener = new OnClickListener() {
		public void onClick(View v) {
			AlertDialog.Builder oDialogSureDeleteBuilder = new AlertDialog.Builder(mainActivity);
			oDialogSureDeleteBuilder
				.setMessage(R.string.lang_sure_delete)
				.setCancelable(false)
				.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						oJdtDb.deleteJob(oJobDbEntry);
						Toast.makeText(mainActivity, R.string.lang_delete_complete, Toast.LENGTH_LONG).show();
					}
				})
				.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {}
				});
			oDialogSureDeleteBuilder.show();
		}
	};
	
	/*
	 * Text watcher for oTxtJobDescription
	 */
	private TextWatcher oWatchTxtJobDescription = new TextWatcher() {
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			
		}
		
		public void afterTextChanged(Editable s) {
			oJobDbEntry.setJobDescription(s.toString());
		}
	};
	
	
	
	///////    Dialogs listeners
	/*
	 * Listeners for date changing
	 */
	private OnDateSetListener oDialogDatePickerDateSetListener = new OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			long iTimestampStartEnd = new GregorianCalendar(year, monthOfYear, dayOfMonth).getTimeInMillis();
			
			oJobDbEntry.setTimestampStart(iTimestampStartEnd);
			oJobDbEntry.setTimestampEnd(iTimestampStartEnd);
			
			populate(oJobDbEntry);
		}
	};
	
	
	
	///////    Constructs
	/*
	 * Construct
	 */
	public JobContentViewAdapter(Activity p_mainActivity, JournalDeTravailDbAdapter p_oJdtDb) {
		mainActivity = p_mainActivity;
		oJdtDb = p_oJdtDb;
		
		oTxtDateFull = (TextView)mainActivity.findViewById(R.id.oTxtDateFull);
		oTxtWeekNumber = (TextView)mainActivity.findViewById(R.id.oTxtWeekNumber);
		oTxtStartEndTime = (TextView)mainActivity.findViewById(R.id.oTxtStartEndTime);
		oTxtTotalTime = (TextView)mainActivity.findViewById(R.id.oTxtTotalTime);
		oTxtType = (TextView)mainActivity.findViewById(R.id.oTxtType);
		oTxtJobDescription = (TextView)mainActivity.findViewById(R.id.oTxtJobDescription);
		
		oBtnEditMode = (ImageButton)mainActivity.findViewById(R.id.oBtnEditMode);
		oBtnEditMode.setOnClickListener(oBtnEditModeClickListener);
		
		oBtnDelJob = (ImageButton)mainActivity.findViewById(R.id.oBtnDelJob);
		oBtnDelJob.setOnClickListener(oBtnDelJobClickListener);
		
		oTxtEditJobDescription = (EditText)mainActivity.findViewById(R.id.oTxtEditJobDescription);
		oTxtEditJobDescription.addTextChangedListener(oWatchTxtJobDescription);
		
		oSpinTypes  = (Spinner)mainActivity.findViewById(R.id.oSpinTypes);
		oSpinnerTypesAdapter = new SpinnerTypesAdapter(mainActivity);
		oSpinTypes.setAdapter(oSpinnerTypesAdapter);
		oSpinTypes.setOnItemSelectedListener(oSpinTypesItemSelectedListener);
		
		oBtnDatePicker = (ImageButton)mainActivity.findViewById(R.id.oBtnDatePicker);
		oBtnDatePicker.setOnClickListener(oBtnDatePickerClickListener);
		
		oBtnStartEndTimePicker  = (ImageButton)mainActivity.findViewById(R.id.oBtnStartEndTimePicker);
		oBtnStartEndTimePicker.setOnClickListener(oBtnStartEndTimePickerClickListener);
		
		oBtnAddType  = (ImageButton)mainActivity.findViewById(R.id.oBtnAddType);
		oBtnAddType.setOnClickListener(oBtnAddTypeClickListener);
		
		oBtnSave = (Button)mainActivity.findViewById(R.id.oBtnSaveModificationsJob);
		oBtnSave.setOnClickListener(oBtnSaveClickListener);
		
		oBtnCancel = (Button)mainActivity.findViewById(R.id.oBtnCancelModificationsJob);
		oBtnCancel.setOnClickListener(oBtnCancelClickListener);
		
		oSpinnerTypesAdapter.populate(p_oJdtDb);
	}
	
	
	
	///////    Methods
	/*
	 * Populate the job view with a job and a type
	 */
	public void populate(JobDbEntry p_oJob, TypeDbEntry p_oType) {
		oJobDbEntry = p_oJob;
		
		oTxtDateFull.setText(p_oJob.getFullDateString());
		oTxtWeekNumber.setText(p_oJob.getWeekNumberString(mainActivity.getString(R.string.lang_week_no)));
		oTxtStartEndTime.setText(p_oJob.getStartEndTimeForJobViewString(mainActivity.getString(R.string.lang_from), mainActivity.getString(R.string.lang_to)));
		oTxtTotalTime.setText(p_oJob.getTotalTimeString(mainActivity.getString(R.string.lang_total)));
		oTxtType.setText(p_oType.getTypeDescription());
		oTxtJobDescription.setText(p_oJob.getJobDescription());
	}
	
	/*
	 * Populate the job view with a job only (no type)
	 */
	public void populate(JobDbEntry p_oJob) {
		oJobDbEntry = p_oJob;
		
		oTxtDateFull.setText(p_oJob.getFullDateString());
		oTxtWeekNumber.setText(p_oJob.getWeekNumberString(mainActivity.getString(R.string.lang_week_no)));
		oTxtStartEndTime.setText(p_oJob.getStartEndTimeForJobViewString(mainActivity.getString(R.string.lang_from), mainActivity.getString(R.string.lang_to)));
		oTxtTotalTime.setText(p_oJob.getTotalTimeString(mainActivity.getString(R.string.lang_total)));
		oTxtType.setText("");
		oTxtJobDescription.setText(p_oJob.getJobDescription());
	}
	
	/*
	 * Switch the job view to edit mode (news buttons appears)
	 */
	public void toEditMode() {
		oBtnDatePicker.setVisibility(ImageButton.VISIBLE);
		oBtnStartEndTimePicker.setVisibility(ImageButton.VISIBLE);
		oBtnAddType.setVisibility(ImageButton.VISIBLE);
		
		oTxtType.setVisibility(TextView.GONE);
		oSpinTypes.setVisibility(Spinner.VISIBLE);
		
		oTxtEditJobDescription.setText(oJobDbEntry.getJobDescription());
		oTxtJobDescription.setVisibility(EditText.GONE);
		oTxtEditJobDescription.setVisibility(EditText.VISIBLE);
		
		oBtnEditMode.setVisibility(ImageButton.GONE);
		oBtnDelJob.setVisibility(ImageButton.GONE);
		oBtnSave.setVisibility(Button.VISIBLE);
		oBtnCancel.setVisibility(Button.VISIBLE);
	}
	
	/*
	 * Switch the job view to normal mode (edit buttons disappears)
	 */
	public void toViewMode() {
		oBtnDatePicker.setVisibility(ImageButton.GONE);
		oBtnStartEndTimePicker.setVisibility(ImageButton.GONE);
		oBtnAddType.setVisibility(ImageButton.GONE);
		
		oSpinTypes.setVisibility(Spinner.GONE);
		oTxtType.setVisibility(TextView.VISIBLE);
		
		oTxtJobDescription.setText(oJobDbEntry.getJobDescription());
		oTxtEditJobDescription.setVisibility(EditText.GONE);
		oTxtJobDescription.setVisibility(EditText.VISIBLE);

		oBtnSave.setVisibility(Button.GONE);
		oBtnCancel.setVisibility(Button.GONE);
		oBtnEditMode.setVisibility(ImageButton.VISIBLE);
		oBtnDelJob.setVisibility(ImageButton.VISIBLE);
		
		populate(oJobDbEntry);
	}
}
